﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web.Admin.test
{
    public partial class [tblName]Edit : Comm.pageBase
    {
        public BLL.[tblName] bll = new BLL.[tblName]();
        public Data.[tblName] m[tblName] = new Data.[tblName]();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
                LoadData();
            }
        }
        
       /// <summary>
       /// 第一次加载页面时候执行
       /// </summary>
       protected void InitPage()
       {

       } 

        /// <summary>
        /// 加载数据
        /// </summary>
        protected void LoadData()
        {
            int.TryParse(Request.QueryString["id"], out keyID);
            if (keyID > 0)
            {
                m[tblName] = bll.GetById(keyID);
            }
            else
            {
                // 新增数据时候表单初始值
            }
            assignForm();
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        protected void SaveData()
        {
            int.TryParse(txtid.Text, out keyID);
            bool isAdd = keyID == 0 ? true : false;

            if (!isAdd)
            {
                m[tblName].id = keyID;
                m[tblName] = bll.GetById(keyID);
            }
            else
            {
                //保存新增数据时的默认值
            }
            readForm();
            if (bll.Update(m[tblName])>0)
            {
                LIB.JsWrite.alertAndCloseUpdateParent("操作成功");
            }
            else
            { LIB.JsWrite.AlertAndGoback("操作失败"); }
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        protected override void delete()
        {
            int id = 0;
            int.TryParse(Request.QueryString["id"], out id);
            if (id > 0)
            {
                var n = bll.Delete(id);
                writeJson(n, "删除成功", "删除失败");
            }
        }

        #region 表单读写
        /// <summary>
        /// 用model数据给表单赋值
        /// </summary>
        protected void assignForm()
        {
            txtid.Text = m[tblName].id.ToString();
[loop start]
            txt[fldName].Text =string.Format("{0}", m[tblName].[fldName]); 
[loop end]
        }

        /// <summary>
        /// 读取表单数据到model
        /// </summary>
        private void readForm()
        {
            #region 数据类型验证
            int varid;	//id
[loop start]
            string var[fldName];	//[fldTitle]
[loop end]
            try
            {
                int.TryParse(txtid.Text.Trim(), out varid);	//id
[loop start]
                var[fldName] = txt[fldName].Text.Trim();	//[fldTitle]
[loop end]
            }
            catch (System.Exception ex)
            {
                 LIB.JsWrite.AlertAndGoback("输入数据格式不合法，请检查输入数据。");
                return;
            }
            #endregion

            #region 赋值model
[loop start]
            m[tblName].[fldName] = Convert.To[LfldType](var[fldName]);	//[fldTitle]
[loop end]
            m[tblName].id = varid;	//id
            #endregion

        }

        #endregion

        /// <summary>
        /// 更新操作
        /// </summary>
        protected void btnSumit_Click(object sender, EventArgs e)
        {
            SaveData();
        }



    }
}



